<template>
  <el-button v-show="allow_submit" @click="submit" :size="size" :type="type" v-bind="$attrs" v-on="$listeners"><slot></slot></el-button>
</template>

<script>
export default {
  data() {
    return {
      pb_text:'',
      allow_submit:false,
    }
  },
  props: {
    nodeins_id: { type: String, default: null },

    size: { type: String, default: 'small' },
    type: { type: String, default: 'warning' },
  },
  watch: {
    nodeins_id: {
      async handler(newval, oldval) {
        if (newval != null && newval!='') {
          //检查是否允许退回
          let CheckPushBackAllow = await elm.post('/system/login/flow/CheckPushBackAllow', {'nodeins_id':this.nodeins_id});
          this.allow_submit = CheckPushBackAllow=='yes';
        }
        else{this.allow_submit = false;}
      },
      immediate: true
    }
  },
  created() {},
  mounted() {
    this.load();
  },
  methods: {
    async load() {
      this.loading = true;
      this.loading = false;
    },
    async submit(){
      this.loading = true;
      if(this.em(this.nodeins_id)){this.allow_submit = false;}

      if(!this.allow_submit){return;}

      if(this.em(this.nodeins_id)){
        this.$message({ type: 'success', message: '缺少数据' });
        this.loading = false;
        return;
      }

      this.$prompt('请输入退回意见', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({ value }) => {
        this.pb_text = value;

        this.$emit('before_submit', value);

        this.$nextTick(function () {
          if(this.allow_submit){ this.v_go_submit();}
        });
      }).catch(() => {});
    },
    async v_go_submit (){
      this.loading = true;

      let result = '';

      let pData = {'nodeins_id':this.nodeins_id,'pb_text':this.pb_text};
      pData['token'] = elm.info.token;
      let data = await elm.post('/system/login/flow/PushBackToNodeins', pData);
      console.log('submit_Pushback'+JSON.stringify(data));
      if(data.status=='success'){
        this.$message({ type: 'success', message: '退回成功' });

        result='success';
      }
      else {
        this.$message.error('退回失败 '+data.message);
        result='failure';
      }

      this.$emit('after_submit', result);

      this.loading = false;
    },

    em: function (s) {
      if (s == null || s == '') { return true; }
      else { return false; }
    },
  }
}
</script>